<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i"  @click="goDetails(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: '',
        flag: false
      };
    },
    onLoad(option) {
      // 把option携带的值复制给queryObj
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.cid || ''
      this.getList()
    },
    methods: {

      async getList(cb) {
        this.flag = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.flag = false
        cb&&cb()
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        console.log(this.queryObj);
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total

      },
      // 页面跳转
      goDetails(goods){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id="+goods.goods_id  
        })
      }
    },
    //上拉加载数据
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
        return uni.$showMsg('数据加载完毕')
      }else{
        if (this.flag) {return}
          else{this.queryObj.pagenum++
          this.getList()}
        }
      },
      //下拉刷新
      onPullDownRefresh(){
        this.queryObj.pagenum=1
        this.goodsList=[]
        this.flag=false
        this.getList(()=>uni.stopPullDownRefresh())
      }
      
  }
</script>

<style lang="scss">

</style>
